package org.liaohailong.library.image.glide

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import org.liaohailong.library.image.ImageEngine
import org.liaohailong.library.image.ImageHelper
import java.io.File

/**
 * Author: liaohailong
 * Date: 2019/3/7
 * Time: 9:34 PM
 * Description: Glide引擎
 **/
class GlideImageEngine : ImageEngine {
    override fun setImage(imageView: ImageView,
                          url: String,
                          width: Int,
                          height: Int,
                          placeHolder: Drawable?,
                          errorDrawable: Drawable?,
                          callback: ImageEngine.OnImageEngineCallback?) {
        GlideApp.with(imageView)
                .load(url)
                .placeholder(placeHolder)
                .error(errorDrawable)
                .override(width, height)
                .into(object : CustomViewTarget<ImageView, Drawable>(imageView) {
                    override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                        imageView.setImageDrawable(resource)
                        callback?.onImageAttachedComplete(imageView, url, true)
                    }

                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        imageView.setImageDrawable(errorDrawable)
                        callback?.onImageAttachedComplete(imageView, url, false)
                    }

                    override fun onResourceCleared(placeholder: Drawable?) {

                    }
                })
    }

    override fun loadImage(context: Context,
                           url: String,
                           path: String,
                           width: Int,
                           height: Int,
                           callback: ImageEngine.OnImageEngineCallback) {

        val requestManager: RequestManager = GlideApp.with(context)
        val requestBuilder: RequestBuilder<File> = requestManager.downloadOnly()
        requestBuilder
                .load(url)
                .listener(object : RequestListener<File> {
                    override fun onResourceReady(resource: File?,
                                                 model: Any?,
                                                 target: Target<File>?,
                                                 dataSource: DataSource?,
                                                 isFirstResource: Boolean): Boolean {

                        ImageHelper.copyFileAsync(resource!!.absolutePath, path, object : ImageHelper.OnImageHelperCallback {
                            override fun onSavedComplete(success: Boolean, path: String) {
                                callback.onImageLoadedComplete(path, url, success)
                            }
                        })
                        return false
                    }

                    override fun onLoadFailed(e: GlideException?,
                                              model: Any?,
                                              target: Target<File>?,
                                              isFirstResource: Boolean): Boolean {
                        callback.onImageLoadedComplete("", url, false)
                        return false
                    }
                })
                .preload(width, height)
    }
}